package com.moke.hit
{
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class HitUtil
	{

		/**
		 * 多物体碰撞检测
		 * (n*n-n)/2
		 */
		public static function multiHit(hitMethod:Function, hitObject:Vector.<Object>):void
		{
			var num:uint = hitObject.length;
			for (var i:uint = 0; i < num - 1; i++)
			{
				var objA:Object = hitObject[i];
				for (var j:uint = i + 1; j < num; j++)
				{
					var objB:Object = hitObject[j];
					hitMethod(objA, objB);
				}
			}
		}

		/** 复杂的碰撞检测 */
		public static function complexHitTestObject(target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1):Boolean
		{
			return (complexIntersectionRectangle(target1, target2, accurracy).width != 0);
		}

		public static function complexIntersectionRectangle(target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1):Rectangle
		{
			if (accurracy <= 0)
				throw new Error("ArgumentError: Error #5001: Invalid value for accurracy", 5001);


			if (!target1.hitTestObject(target2))
				return new Rectangle();

			var hitRectangle:Rectangle = intersectionRectangle(target1, target2);

			if (hitRectangle.width * accurracy < 1 || hitRectangle.height * accurracy < 1)
				return new Rectangle();

			var bitmapData:BitmapData = new BitmapData(hitRectangle.width * accurracy, hitRectangle.height * accurracy, false, 0x000000);

			bitmapData.draw(target1, getDrawMatrix(target1, hitRectangle, accurracy), new ColorTransform(1, 1, 1, 1, 255, -255, -255, 255));

			bitmapData.draw(target2, getDrawMatrix(target2, hitRectangle, accurracy), new ColorTransform(1, 1, 1, 1, 255, 255, 255, 255), BlendMode.DIFFERENCE);


			var intersection:Rectangle = bitmapData.getColorBoundsRect(0xFFFFFFFF, 0xFF00FFFF);

			bitmapData.dispose();


			if (accurracy != 1)
			{
				intersection.x /= accurracy;
				intersection.y /= accurracy;
				intersection.width /= accurracy;
				intersection.height /= accurracy;
			}

			intersection.x += hitRectangle.x;
			intersection.y += (hitRectangle.y);

			return intersection;
		}

		public static function intersectionRectangle(target1:DisplayObject, target2:DisplayObject):Rectangle
		{

			if (!target1.root || !target2.root || !target1.hitTestObject(target2))
				return new Rectangle();


			var bounds1:Rectangle = target1.getBounds(target1.root);
			var bounds2:Rectangle = target2.getBounds(target2.root);


			var intersection:Rectangle = new Rectangle();
			intersection.x = Math.max(bounds1.x, bounds2.x);
			intersection.y = Math.max(bounds1.y, bounds2.y);
			intersection.width = Math.min((bounds1.x + bounds1.width) - intersection.x, (bounds2.x + bounds2.width) - intersection.x);
			intersection.height = Math.min((bounds1.y + bounds1.height) - intersection.y, (bounds2.y + bounds2.height) - intersection.y);

			return intersection;
		}


		protected static function getDrawMatrix(target:DisplayObject, hitRectangle:Rectangle, accurracy:Number):Matrix
		{
			var localToGlobal:Point;
			var matrix:Matrix;

			var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix;

			localToGlobal = target.localToGlobal(new Point());
			matrix = target.transform.concatenatedMatrix;
			matrix.tx = localToGlobal.x - hitRectangle.x;
			matrix.ty = localToGlobal.y - hitRectangle.y;

			matrix.a = matrix.a / rootConcatenatedMatrix.a;
			matrix.d = matrix.d / rootConcatenatedMatrix.d;
			if (accurracy != 1)
				matrix.scale(accurracy, accurracy);

			return matrix;
		}
	}
}

/**
 * 使用BitmapData.hitTest 来检测
 *
 * hitTest(firstPoint:Point,
 * 			firstAlphaThreshold:uint,
 * 			secondObject:Object,
 * 			secondPoint:Point,
 * 			secondAlphaThreshold:uint)
 *
 *
 *
 */
